﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Repetition.Domain;
using Repetition.Views.Controls;

namespace Repetition.Views
{
    public partial class AddProblemDialog : Form
    {
        // Controls
        private ProblemDifficultyImportanceView difficultyImportanceView;
        private TypedListView<Subject> subjectList = new TypedListView<Subject>(x => x.Name);

        // Model members
        private ICollection<Subject> subjects;
        private Problem problem;

        public AddProblemDialog(ICollection<Subject> availableSubjects)
            : this(availableSubjects, new Problem()) { }

        public AddProblemDialog(ICollection<Subject> availableSubjects, Problem problem)
        {
            InitializeComponent();

            questionTextbox.Text = problem.Question;
            answerTextbox.Text = problem.Answer;

            this.subjects = availableSubjects;
            this.problem = new Problem();
            this.problem.Copy(problem);

            subjectList.View = View.List;
            subjectList.Dock = DockStyle.Fill;
            subjectList.HideSelection = false;

            difficultyImportanceView = new ProblemDifficultyImportanceView(ProblemDifficultyOrImportanceChangedEvent);
            difficultyImportanceView.Problem = this.problem;

            subjectsGroupBox.Controls.Add(subjectList);
            tableLayoutPanel.Controls.Add(difficultyImportanceView, 2, 0);
            tableLayoutPanel.SetRowSpan(difficultyImportanceView, 2);
        }

        private void AddProblem_Load(object sender, EventArgs e)
        {
            foreach (var subject in subjects)
                subjectList.Add(subject);

            foreach (var subject in problem.Subjects)
                subjectList.SelectItem(subject);
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        public Problem Problem
        {
            get
            {
                UpdateProblemFromForm(problem);
                return problem;
            }
        }

        private void UpdateProblemFromForm(Problem problem)
        {
            problem.Question = questionTextbox.Text;
            problem.Answer = answerTextbox.Text;

            problem.RemoveAllSubjects();

            var SelectedSubjects = subjectList.SelectedItems;
            foreach (Subject subject in SelectedSubjects)
                problem.AddSubject(subject);
        }

        private void ProblemDifficultyOrImportanceChangedEvent()
        {
            // Nothing needs to be done, the reference is updated by the ProblemDifficultyImportanceView
        }
    }
}
